<template>
	<view class="content">
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex == index}"
			 @click="tabClick(index)">
				{{item.text}}
			</view>
		</view>
		<view class="swiper-box">
			<view class="tab-content">
				<me-scroll-uni :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @init="mescrollInit" bottom="0"
				 top="70" class="list-scroll-content">
					<!-- 订单列表 -->
					<view v-for="(item,index) in orderList" :key="index" class="order-item">
						<view class="b-b">
							<text class="time">订单号：{{item.order_no}}</text>
						</view>
						
						<view class="i-top">
							<text class="time">{{item.add_time}}</text>
							<text class="state" :style="{color: item.stateTipColor}">{{item.status_name}}</text>
							<text v-if="item.status==5 || item.status==6" class="del-btn iconfont icon-shanchu1" @click="deleteOrder(item,index)"></text>
						</view>

						<scroll-view v-if="item.items_count > 1" class="goods-box" scroll-x>
							<view v-for="(goodsItem, goodsIndex) in item.items" :key="goodsIndex" class="goods-item">
								<image class="goods-img" :src="goodsItem.images" mode="aspectFill"></image>
							</view>
						</scroll-view>
						<view v-if="item.items_count === 1" class="goods-box-single" v-for="(goodsItem, goodsIndex) in item.items" :key="goodsIndex">
							<image class="goods-img" :src="goodsItem.images" mode="aspectFill"></image>
							<view class="right">
								<text class="title ellipsis">{{goodsItem.title}}</text>
								<text class="attr-box">{{goodsItem.spec_text}} x {{goodsItem.buy_number}}</text>
								<text class="price">{{goodsItem.price}}</text>
							</view>
						</view>

						<view class="price-box">
							共
							<text class="num">{{item.buy_number_count}}</text>
							件商品 实付款
							<text class="price">{{item.total_price}}</text>
						</view>
						<view class="action-box b-t" v-if="item.status == 1">
							<button class="action-btn" @click="cancelOrder(item,index)">取消订单</button>
							<button class="action-btn recom" @tap="pay(item,index)">立即支付</button>
						</view>
						<view class="action-box b-t" v-if="item.status == 2">
							<button class="action-btn" @click="refundOrder(item)">申请退单</button>
						</view>
						<view class="action-box b-t" v-if="item.status == 3">
							<button class="action-btn" @click="confirmReceived(item,index)">确认收货</button>
						</view>
						<view class="action-box b-t" v-if="item.user_is_comments == 0 && item.status==4">
							<button class="action-btn" @click="comments(item)">待评价</button>
						</view>
					</view>

				</me-scroll-uni>


			</view>
		</view>

	</view>
</template>

<script>
	import meScrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
	export default {
		components: {
			meScrollUni,
		},
		data() {
			return {
				tabCurrentIndex: 0,
				status: 0,
				orderList: [],
				navList: [{
						state: 0,
						text: '全部',
					},
					{
						state: 1,
						text: '待付款',
					},
					{
						state: 2,
						text: '待发货',
					},
					{
						state: 3,
						text: '待收货',
					},
					{
						state: 4,
						text: '待评价',
					}
				],

				mescroll: null, // mescroll实例对象
				// 下拉刷新的常用配置
				downOption: {
					use: true, //是否启用下拉刷新;默认true
					auto: true //是否在初始化完毕之后自动执行下拉刷新的回调;默认true 
				},
				// 上拉加载的常用配置
				upOption: {
					use: true, //是否启用上拉加载;默认true
					auto: true, //是否在初始化完毕之后自动执行上拉加载的回调,默认true
					page: {
						num: 0, //当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 //每页数据的数量，默认10
					},
					noMoreSize: 3, //配置列表的总数量要大于5条才显示‘--END--’的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
			};
		},

		onLoad(options) {
			/**
			 * 修复app端点击除全部订单外的按钮进入时不加载数据的问题
			 * 替换onLoad下代码即可
			 */
			if (options.state == 0) {
				this.status = [0, 1, 2, 3, 4, 5, 6];
			} else {
				this.status = options.state;
				this.tabCurrentIndex=this.status;
			}

		},

		methods: {

			// mescroll组件初始化的回调,可获取到mescroll对象
			mescrollInit(mescroll) {
				this.mescroll = mescroll
			},
			// 下拉刷新的回调
			downCallback(mescroll) {
				setTimeout(function() {
					mescroll.endSuccess();
				}, 1000);
			},
			// 上拉加载的回调
			upCallback(mescroll) {
				// 此时mescroll会携带page的参数：
				let pageNum = mescroll.num; // 页码, 默认从1开始
				let pageSize = mescroll.size; // 页长, 默认每页10条
				this.loadData(pageNum, pageSize, mescroll);

			},
			//加载订单 ，带下拉刷新和上滑加载
			loadData(pageNum = 0, pageSize = 10, mescroll = []) {
				var self = this;
				
				// 获取商品信息的参数,如果选中的是第四个选项，及未评价，则用is_comments这个参数。其他的状态不用
				if(this.status==4){
					var param = {
						data: {
							user_type:'user',
							is_comments: 0,
							is_more: 1,
							page:pageNum
						}
					};
				}else{
					var param = {
						data: {
							status: this.status,
							is_more: 1,
							page:pageNum
						}
					};
				}
				
				// 获取商品列表信息
				var orderListUrl = this.requestUrl.orderList;
				this.util.request(orderListUrl, 'post', function(res) {
					if (res.data.code == 0 && res.data.data) {
						let curPageData = res.data.data.data;
						let totalPage = res.data.data.page_total;
						//设置列表数据
						//追加新数据
						self.orderList = self.orderList.concat(curPageData);
						self.orderList.forEach(item => {
							//添加不同状态下订单的表现形式
							Object.assign(item, self.orderStateExp(item.status));
						})
						// 最后一页了，显示end
						mescroll.endByPage(curPageData.length, totalPage);
					}
				}, param);
			},


			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index;
				if (this.tabCurrentIndex == 0) {
					this.status = [0, 1, 2, 3, 4, 5, 6];
				} else {
					this.status = this.tabCurrentIndex;
				}
				this.orderList = []; // 在这里手动置空列表
				this.mescroll.resetUpScroll(); // 刷新列表数据
			},
			//删除订单
			deleteOrder(item,index) {
				let self=this;
				let param={
					data:{
						id:item.id,
					}
				}
				let delOrderUrl=this.requestUrl.delOrder;
				this.util.request(delOrderUrl,'post',function(res){
					if(res.data.code==0){
						uni.showToast({
							title: res.data.msg,
							duration:1500
						});
						self.orderList.splice(index,1);
					}
				},param);
				
			},
			//取消订单
			cancelOrder(item,index) {
				let self=this;
				let param={
					data:{
						id:item.id
					}
				}
				let cancelOrderUrl=this.requestUrl.cancleOrder;
				this.util.request(cancelOrderUrl,'post',function(res){
					if(res.data.code==0){
						uni.showToast({
							title: res.data.msg,
							duration:1500
						});
						let temp=JSON.parse(JSON.stringify(self.orderList));
						temp[index].status=5;
						temp[index].status_name='已取消';
						temp[index].stateTipColor='#f55719';
						self.orderList=temp;
					}
				},param);
			},
			// 确认收货
			confirmReceived(item,index){
				let self=this;
				let param={
					data:{
						id:item.id
					}
				}
				let collectOrderUrl=this.requestUrl.collectOrder;
				this.util.request(collectOrderUrl,'post',function(res){
					if(res.data.code==0){
						uni.showToast({
							title: res.data.msg,
							duration:1500
						});
						let temp=JSON.parse(JSON.stringify(self.orderList));
						temp[index].status=4;
						temp[index].status_name='已完成';
						temp[index].stateTipColor='#18cf8c';
						self.orderList=temp;
					}
				},param)
			},
			// 用户评价
			comments(item){
				uni.navigateTo({
					url:`/pages/comments/comments?id=${item.id}`
				})
			},
			// 去支付
			pay(item){
				let paymentUrl=this.requestUrl.payment;
				let params={
					data:{
						payment_id:item.payment_id
					}
				}
				let payment='';
				this.util.request(paymentUrl,'post',function(res){
					if(res.data.code==0){
						payment=res.data.data
						uni.navigateTo({
							url:`/pages/pay/pay?id=${item.id}&payment_id=${item.payment_id}&payment=${payment}`
						})
					}
				},params)
				
			},
			// 申请退单
			refundOrder(item){
				let orderId=item.id
				uni.navigateTo({
					url: `/pages/refund/refund?orderid=${orderId}`,
				});
			},
			//订单状态文字和颜色
			orderStateExp(state) {
				state=parseInt(state);
				let stateTip = '',
					stateTipColor = '#fa436a';
				switch (state) {
					case 1:
						stateTip = '待付款';
						break;
					case 2:
						stateTip = '待发货';
						break;
					case 3:
						stateTip = '待收货';
						stateTipColor = '#12bac0';
						break;
					case 4:
						stateTip = '已完成';
						stateTipColor = '#18cf8c';
						break;
					case 5:
						stateTip = '已取消';
						stateTipColor = '#f55719';
						break;
					case 6:
						stateTip = '订单已关闭';
						stateTipColor = '#909399';
						break;

						//更多自定义
				}
				return {
					stateTip,
					stateTipColor
				};
			}
		},
	}
</script>

<style lang="scss">
	page,
	.content {
		background: $uni-bg-color-grey;
		height: 100%;
	}

	.swiper-box {
		height: calc(100% - 40px);
	}

	.list-scroll-content {
		height: 100%;
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, .06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $uni-text-color;
			position: relative;

			&.current {
				color: $uni-text-color-zhu;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $uni-text-color-zhu;
				}
			}
		}
	}

	.uni-swiper-item {
		height: auto;
	}

	.order-item {
		display: flex;
		flex-direction: column;
		padding-left: 30upx;
		background: #fff;
		margin-top: 16upx;

		.i-top {
			display: flex;
			align-items: center;
			padding-bottom: 10rpx;
			padding-right: 30upx;
			font-size: $uni-font-size-base;
			color: $uni-text-color;
			position: relative;

			.time {
				flex: 1;
				line-height: 60rpx;
				color: $uni-text-color-grey;
			}

			.state {
				color: $uni-text-color;
			}

			.del-btn {
				padding: 10upx 0 10upx 36upx;
				font-size: $uni-font-size-lg;
				color: $uni-text-color;
				position: relative;

				&:after {
					content: '';
					width: 0;
					height: 30upx;
					border-left: 1px solid $uni-text-color;
					position: absolute;
					left: 20upx;
					top: 50%;
					transform: translateY(-50%);
				}
			}
		}
		
		.b-b{padding-top: 30rpx;}

		/* 多条商品 */
		.goods-box {
			height: 160upx;
			padding: 20upx 0;
			white-space: nowrap;
			box-sizing: border-box;

			.goods-item {
				width: 120upx;
				height: 120upx;
				display: inline-block;
				margin-right: 24upx;
			}

			.goods-img {
				display: block;
				width: 100%;
				height: 100%;
			}
		}

		/* 单条商品 */
		.goods-box-single {
			display: flex;
			padding: 20upx 0;

			.goods-img {
				display: block;
				width: 120upx;
				height: 120upx;
			}

			.right {
				flex: 1;
				display: flex;
				flex-direction: column;
				padding: 0 30upx 0 24upx;
				overflow: hidden;

				.title {
					font-size: $uni-font-size-base;
					color: $uni-text-color;
					line-height: 1;
				}

				.attr-box {
					font-size: $uni-font-size-sm + 2upx;
					color: $uni-text-color-grey;
					padding: 10upx 12upx;
				}

				.price {
					font-size: $uni-font-size-base + 2upx;
					color: $uni-text-color;

					&:before {
						content: '￥';
						font-size: $uni-font-size-sm;
						margin: 0 2upx 0 8upx;
					}
				}
			}
		}

		.price-box {
			display: flex;
			justify-content: flex-end;
			align-items: baseline;
			padding: 20upx 30upx;
			font-size: $uni-font-size-sm + 2upx;
			color: $uni-text-color-grey;
			border-bottom: solid 1px $uni-border-color;

			.num {
				margin: 0 8upx;
				color: $uni-text-color;
			}

			.price {
				font-size: $uni-font-size-lg;
				color: $uni-text-color;

				&:before {
					content: '￥';
					font-size: $uni-font-size-sm;
					margin: 0 2upx 0 8upx;
				}
			}
		}

		.action-box {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			height: 100upx;
			position: relative;
			padding-right: 30upx;
		}

		.action-btn {
			width: 160upx;
			height: 60upx;
			margin: 0;
			margin-left: 24upx;
			padding: 0;
			text-align: center;
			line-height: 60upx;
			font-size: $uni-font-size-sm + 2upx;
			color: #f55719;
			background: #fff;
			border-radius: 100px;
			
			&:after {
				border-radius: 100px;border-color: #f55719;
			}

			&.recom {
				background: #fff9f9;
				color: $uni-text-color-zhu;

				&:after {
					border-color: #f7bcc8;
				}
			}
		}
	}


	/* load-more */
	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80upx;
		align-items: center;
		justify-content: center
	}

	.uni-load-more__text {
		font-size: 28upx;
		color: #999
	}

	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}

	.uni-load-more__img>view {
		position: absolute
	}

	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}

	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}

	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}

	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}

	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}

	.load2 {
		transform: rotate(30deg)
	}

	.load3 {
		transform: rotate(60deg)
	}

	.load1 view:nth-child(1) {
		animation-delay: 0s
	}

	.load2 view:nth-child(1) {
		animation-delay: .13s
	}

	.load3 view:nth-child(1) {
		animation-delay: .26s
	}

	.load1 view:nth-child(2) {
		animation-delay: .39s
	}

	.load2 view:nth-child(2) {
		animation-delay: .52s
	}

	.load3 view:nth-child(2) {
		animation-delay: .65s
	}

	.load1 view:nth-child(3) {
		animation-delay: .78s
	}

	.load2 view:nth-child(3) {
		animation-delay: .91s
	}

	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}

	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}

	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}

	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}

	@-webkit-keyframes load {
		0% {
			opacity: 1
		}

		100% {
			opacity: .2
		}
	}
</style>
